package com.example.fbi_text.views;

import static android.app.Activity.RESULT_OK;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.example.fbi_text.Acitivity.ChangePasswordActivity;
import com.example.fbi_text.Acitivity.RegisterActivity;


import com.example.fbi_text.R;


import com.example.fbi_text.adapter.UsersAdapter;
import com.example.fbi_text.base.BaseFragment;
import com.example.fbi_text.bean.User;
import com.example.fbi_text.database.DBHelper;

import java.util.List;

public class AccountFragment extends BaseFragment implements View.OnClickListener {
    private ImageButton madd_account;
    private ListView Users;
    private List<User> UserList;
    private UsersAdapter UserAdapter;
    private AlertDialog mDialog;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_account_list;
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void initView() {
        madd_account=mView.findViewById(R.id.add_account);
        Users=mView.findViewById(R.id.account_list_view);
        // 隐藏头部
        View header = mView.findViewById(R.id.account_header);
        if (header != null) {
            header.setVisibility(View.GONE);
        }
        //数据库查寻用户列表，显示在账号管理界面
        UserList= DBHelper.getInstance(mContext).selectAllStudent();
        UserAdapter=new UsersAdapter(UserList,mContext);
        Users.setAdapter(UserAdapter);
        //短按
        Users.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                User user = (User) view.getTag(R.integer.user_tag);
                // 跳转到修改密码页面
                Intent intent = new Intent(mContext, ChangePasswordActivity.class);
                intent.putExtra("User_id", user.getUser_id()); // 传递账户ID
                startActivityForResult(intent, 1);
            }
        });

        //长按删除
        Users.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                User user = (User) view.getTag(R.integer.user_tag);
                showDeleteDialog(user);
                return true;
            }
        });
    }
    private void showDeleteDialog(User user) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.dialog_attention, null);
        TextView message = v.findViewById(R.id.message);
        Button btn_sure = v.findViewById(R.id.button_right);
        Button btn_cancel = v.findViewById(R.id.button_left);

        message.setText("确定要删除账号 " + user.getUser_name() + " 吗？");

        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.cancel();
                DBHelper.getInstance(mContext).deleteUserById(user.getUser_id());
                showToast(user.getUser_name()+ " 账号已删除");
                refreshAccountList();
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.cancel();
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        mDialog = builder.create();
        mDialog.show();

        Window window = mDialog.getWindow();
        if (window != null) {
            window.setLayout(600, 400);
            window.setContentView(v);
        }
    }
    // 添加onActivityResult方法处理返回结果
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            refreshAccountList(); // 密码修改成功后刷新列表
        }
    }
    private void refreshAccountList() {
        Log.d("AccountFragment", "Refreshing account list");
        UserList = DBHelper.getInstance(mContext).selectAllStudent();
        if (UserAdapter == null) {
            UserAdapter = new UsersAdapter(UserList, mContext);
            Users.setAdapter(UserAdapter);
        } else {
            UserAdapter.updateData(UserList);
            UserAdapter.notifyDataSetChanged();
        }
    }


    @Override
    protected void initListener() {
        madd_account.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id==R.id.add_account){
            Intent intent=new Intent(mContext, RegisterActivity.class);
            startActivity(intent);
        }
    }
}
